home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / c++ / cursesp.h < prev    next >
C/C++ Source or Header  |  2002-10-24  |  8KB  |  223 lines

  1. // * This makes emacs happy -*-Mode: C++;-*-
  2. /****************************************************************************
  3.  * Copyright (c) 1998,1999,2000,2001 Free Software Foundation, Inc.         *
  4.  *                                                                          *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  6.  * copy of this software and associated documentation files (the            *
  7.  * "Software"), to deal in the Software without restriction, including      *
  8.  * without limitation the rights to use, copy, modify, merge, publish,      *
  9.  * distribute, distribute with modifications, sublicense, and/or sell       *
  10.  * copies of the Software, and to permit persons to whom the Software is    *
  11.  * furnished to do so, subject to the following conditions:                 *
  12.  *                                                                          *
  13.  * The above copyright notice and this permission notice shall be included  *
  14.  * in all copies or substantial portions of the Software.                   *
  15.  *                                                                          *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  18.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  19.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  20.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  21.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  22.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  23.  *                                                                          *
  24.  * Except as contained in this notice, the name(s) of the above copyright   *
  25.  * holders shall not be used in advertising or otherwise to promote the     *
  26.  * sale, use or other dealings in this Software without prior written       *
  27.  * authorization.                                                           *
  28.  ****************************************************************************/
  29.  
  30. /****************************************************************************
  31.  *   Author: Juergen Pfeifer, 1997                                          *
  32.  *   Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en             *
  33.  ****************************************************************************/
  34.  
  35. #ifndef NCURSES_CURSESP_H_incl
  36. #define NCURSES_CURSESP_H_incl 1
  37.  
  38. // $Id: cursesp.h,v 1.17 2002/07/06 15:47:52 juergen Exp $
  39.  
  40. #include <cursesw.h>
  41.  
  42. extern "C" {
  43. #  include <panel.h>
  44. }
  45.  
  46. class NCURSES_IMPEXP NCursesPanel : public NCursesWindow {
  47. protected:
  48.   PANEL *p;
  49.   static NCursesPanel *dummy;
  50.  
  51. private:
  52.   // This structure is used for the panel's user data field to link the
  53.   // PANEL* to the C++ object and to provide extra space for a user pointer.
  54.   typedef struct {
  55.     void*               m_user;      // the pointer for the user's data
  56.     const NCursesPanel* m_back;      // backward pointer to C++ object
  57.     const PANEL*        m_owner;     // the panel itself
  58.   } UserHook;
  59.  
  60.   void init();                       // Initialize the panel object
  61.  
  62. protected:
  63.   void set_user(void *user) {
  64.     UserHook* uptr = (UserHook*)::panel_userptr (p);
  65.     assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==p);
  66.     uptr->m_user = user;
  67.   }
  68.   // Set the user pointer of the panel.
  69.   
  70.   void *get_user() {
  71.     UserHook* uptr = (UserHook*)::panel_userptr (p);
  72.     assert (uptr != 0 && uptr->m_back==this && uptr->m_owner==p);
  73.     return uptr->m_user;
  74.   }
  75.   
  76.   void OnError (int err) const THROWS((NCursesPanelException)) {
  77.     if (err==ERR)
  78.       THROW(new NCursesPanelException (this, err));
  79.   }
  80.   // If err is equal to the curses error indicator ERR, an error handler
  81.   // is called.
  82.  
  83.   // Get a keystroke. Default implementation calls getch()
  84.   virtual int getKey(void);
  85.  
  86. public:
  87.   NCursesPanel(int lines,
  88.            int cols,
  89.            int begin_y = 0,
  90.            int begin_x = 0)
  91.     : NCursesWindow(lines,cols,begin_y,begin_x) {
  92.       init();
  93.   } 
  94.   // Create a panel with this size starting at the requested position.
  95.  
  96.   NCursesPanel() : NCursesWindow(::stdscr) { init(); }
  97.   // This constructor creates the default Panel associated with the
  98.   // ::stdscr window
  99.  
  100.   virtual ~NCursesPanel();
  101.   
  102.   // basic manipulation
  103.   inline void hide() {
  104.     OnError (::hide_panel(p));
  105.   }
  106.   // Hide the panel. It stays in the stack but becomes invisible.
  107.  
  108.   inline void show() {
  109.     OnError (::show_panel(p));
  110.   }
  111.   // Show the panel, i.e. make it visible.
  112.  
  113.   inline void top() {
  114.     OnError (::top_panel(p));
  115.   }
  116.   // Make this panel the top panel in the stack.
  117.   
  118.   inline void bottom() {
  119.     OnError (::bottom_panel(p));
  120.   }
  121.   // Make this panel the bottom panel in the stack.
  122.   // N.B.: The panel associated with ::stdscr is always on the bottom. So
  123.   // actually bottom() makes the panel the first above ::stdscr.
  124.   
  125.   virtual int mvwin(int y, int x) {
  126.     OnError(::move_panel(p, y, x));
  127.     return OK;
  128.   }
  129.   
  130.   inline bool hidden() const {
  131.     return (::panel_hidden (p) ? TRUE : FALSE);
  132.   }
  133.   // Return TRUE if the panel is hidden, FALSE otherwise.
  134.  
  135. /* The functions panel_above() and panel_below() are not reflected in
  136.    the NCursesPanel class. The reason for this is, that we cannot
  137.    assume that a panel retrieved by those operations is one wrapped
  138.    by a C++ class. Although this situation might be handled, we also
  139.    need a reverse mapping from PANEL to NCursesPanel which needs some
  140.    redesign of the low level stuff. At the moment, we define them in the
  141.    interface but they will always produce an error. */
  142.   inline NCursesPanel& above() const {
  143.     OnError(ERR);
  144.     return *dummy;
  145.   }
  146.  
  147.   inline NCursesPanel& below() const {
  148.     OnError(ERR);
  149.     return *dummy;
  150.   }
  151.  
  152.   // Those two are rewrites of the corresponding virtual members of
  153.   // NCursesWindow
  154.   virtual int refresh();
  155.   // Propagate all panel changes to the virtual screen and update the 
  156.   // physical screen.
  157.  
  158.   virtual int noutrefresh();
  159.   // Propagate all panel changes to the virtual screen.
  160.  
  161.   static void redraw();
  162.   // Redraw all panels.
  163.  
  164.   // decorations
  165.   virtual void frame(const char* title=NULL, 
  166.              const char* btitle=NULL);
  167.   // Put a frame around the panel and put the title centered in the top line
  168.   // and btitle in the bottom line.
  169.  
  170.   virtual void boldframe(const char* title=NULL,
  171.              const char* btitle=NULL);
  172.   // Same as frame(), but use highlighted attributes.
  173.  
  174.   virtual void label(const char* topLabel,
  175.              const char* bottomLabel);
  176.   // Put the title centered in the top line and btitle in the bottom line.
  177.  
  178.   virtual void centertext(int row,const char* label);
  179.   // Put the label text centered in the specified row.
  180. };
  181.  
  182. /* We use templates to provide a typesafe mechanism to associate
  183.  * user data with a panel. A NCursesUserPanel<T> is a panel 
  184.  * associated with some user data of type T.
  185.  */
  186. template<class T> class NCursesUserPanel : public NCursesPanel
  187. {
  188. public:
  189.   NCursesUserPanel (int lines,
  190.             int cols,
  191.             int begin_y = 0,
  192.             int begin_x = 0,
  193.             const T* p_UserData = (T*)0)
  194.     : NCursesPanel (lines, cols, begin_y, begin_x) {
  195.       if (p)
  196.     set_user ((void *)p_UserData);
  197.   };
  198.   // This creates an user panel of the requested size with associated
  199.   // user data pointed to by p_UserData.
  200.   
  201.   NCursesUserPanel(const T* p_UserData = (T*)0) : NCursesPanel() {
  202.     if (p)
  203.       set_user((void *)p_UserData);
  204.   };
  205.   // This creates an user panel associated with the ::stdscr and user data
  206.   // pointed to by p_UserData.
  207.  
  208.   virtual ~NCursesUserPanel() {};
  209.  
  210.   T* UserData (void) const {
  211.     return (T*)get_user ();
  212.   };
  213.   // Retrieve the user data associated with the panel.
  214.   
  215.   virtual void setUserData (const T* p_UserData) {
  216.     if (p)
  217.       set_user ((void *)p_UserData);
  218.   }
  219.   // Associate the user panel with the user data pointed to by p_UserData.
  220. };
  221.  
  222. #endif // NCURSES_CURSESP_H_incl
  223.